package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.Coupon;
import com.itheima.domain.CouponProductCategoryRelation;
import com.itheima.domain.CouponProductRelation;
import com.itheima.service.SmsCouponProductCategoryRelationService;
import com.itheima.service.SmsCouponProductRelationService;
import com.itheima.service.SmsCouponService;
import com.itheima.util.Result;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SmsCouponVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SmsCouponManager {

    @DubboReference
    SmsCouponService smsCouponService;
    @DubboReference
    SmsCouponProductCategoryRelationService smsCouponProductCategoryRelationService;
    @DubboReference
    SmsCouponProductRelationService smsCouponProductRelationService;

    //根据优惠券名称和类型分页获取优惠券列表
    public Result list(Integer pageNum, Integer pageSize, String name, Integer type) {
        PageBeanVo list = smsCouponService.list(pageNum, pageSize, name, type);

        return new Result(true, 200, "操作成功", list);

    }

    //添加优惠券
    public Result create(SmsCouponVo couponParam) {
        Coupon smsCoupon = new Coupon();
        BeanUtil.copyProperties(couponParam, smsCoupon);
        smsCouponService.save(smsCoupon);

        List<CouponProductCategoryRelation> productCategoryRelationList = couponParam.getProductCategoryRelationList();
        if (CollUtil.isNotEmpty(productCategoryRelationList)) {
            for (CouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelation.setCouponId(smsCoupon.getId());
                smsCouponProductCategoryRelationService.save(smsCouponProductCategoryRelation);
            }
        }

        List<CouponProductRelation> productRelationList = couponParam.getProductRelationList();
        if (CollUtil.isNotEmpty(productRelationList)) {
            for (CouponProductRelation smsCouponProductRelation : productRelationList) {
                smsCouponProductRelation.setCouponId(smsCoupon.getId());
                smsCouponProductRelationService.save(smsCouponProductRelation);
            }
        }
        return new Result(true, 200, "操作成功", 1);
    }

    //删除优惠券
    public Result delete(Integer id) {
        smsCouponService.delete(id);
        smsCouponProductRelationService.delete(id);
        smsCouponProductCategoryRelationService.delete(id);

        return new Result(true, 200, "操作成功", 1);
    }

    //修改优惠券
    public Result update(Integer id, SmsCouponVo couponParam) {
        Coupon smsCoupon = new Coupon();
        BeanUtil.copyProperties(couponParam, smsCoupon);
        smsCoupon.setId(Long.valueOf(id));
        smsCouponService.update(smsCoupon);

//另外俩

        return new Result(true, 200, "操作成功", 1);
    }

    //获取单个优惠券的详细信息
    public Result selectById(Long id) {

        Coupon smsCoupon = smsCouponService.selectById(id);
        List<CouponProductRelation> productRelationList = smsCouponProductRelationService.selectyId(id);
        List<CouponProductCategoryRelation> productCategoryRelationList = smsCouponProductCategoryRelationService.selectById(id);

        SmsCouponVo vo = new SmsCouponVo();
        BeanUtil.copyProperties(smsCoupon, vo);
        vo.setProductRelationList(productRelationList);
        vo.setProductCategoryRelationList(productCategoryRelationList);

        return new Result(true, 200, "操作成功", vo);
    }
}
